package com.chuangshu.chuangshuteam.controller;


import com.chuangshu.chuangshuteam.annotation.RequiresRoles;
import com.chuangshu.chuangshuteam.dto.ApplicationDto;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.BusinessKey;
import com.chuangshu.chuangshuteam.service.ApplicationsService;
import com.chuangshu.chuangshuteam.vo.ApplicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/applications")
public class ApplicationsController {

    @Autowired
    private ApplicationsService applicationsService;

    @PostMapping("/submit")
    public Result submitApplication(@RequestBody ApplicationVo applicationVo) {
        log.info("用户提交申请 {}", applicationVo);
        if (!BusinessKey.isValidDirection(applicationVo.getDirection())) {
            return Result.fail("请填写正确的申请方向");
        }
        return applicationsService.submitApplication(applicationVo);
    }
    /**
     * 分页查询所有申请
     * @param page 页码，默认为1
     * @param pageSize 每页大小，默认为10
     * @return 分页结果
     */
    @RequiresRoles("member")
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required = false,defaultValue = "")
                       String direction
                       ) {
        log.info("获取所有申请，页码: {}, 每页大小: {}", page, pageSize);
        Result result = applicationsService.getByPage(page, pageSize,direction);
        return result;
    }

    /**
     * 根据招聘周期ID查询所有申请
     * @param recruitmentCycleId 招聘周期ID
     * @return 申请列表
     */
    @RequiresRoles("member")
    @GetMapping("/query/{recruitmentCycleId}")
    public Result getApplicationsByRecruitmentCycleId(@PathVariable Long recruitmentCycleId) {
        log.info("根据招聘周期ID查询申请: {}", recruitmentCycleId);
        List<ApplicationDto> applications = applicationsService.getApplicationsByRecruitmentCycleId(recruitmentCycleId);
        return Result.ok(applications);
    }

    /**
     * 根据申请ID查询详细信息（包含关联文件）
     * @param id 申请ID
     * @return 申请详细信息
     */
    @GetMapping("/detail/{id}")
    public Result getApplicationDetailById(@PathVariable Long id) {
        log.info("根据ID查询申请详情: {}", id);
        ApplicationDto application = applicationsService.getApplicationDetailById(id);
        if (application == null) {
            return Result.fail("申请不存在");
        }
        return Result.ok(application);
    }


    /**
     * 更新申请信息
     * @param applicationVo 申请信息
     * @return 更新结果
     */
    @RequiresRoles("member")
    @PutMapping("/update")
    public Result updateApplication(@RequestBody ApplicationVo applicationVo) {
        log.info("更新申请信息: {}", applicationVo);
        Result result = applicationsService.updateApplication(applicationVo);
        return result;
    }

    /**
     * 删除申请及其关联文件
     * @param id 申请ID
     * @return 删除结果
     */
    @RequiresRoles("member")
    @DeleteMapping("/delete/{id}")
    public Result deleteApplication(@PathVariable Long id) {
        log.info("删除申请: {}", id);
        Result result = applicationsService.deleteApplication(id);
        return result;
    }
    @GetMapping("/query/self")
    public Result getSelfApplications() {
        log.info("获取用户自己的申请");
        Result result = applicationsService.getSelfApplications();
        return result;
    }
    /**
     * 用户删除自己的申请
     * @param id 申请ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/self/{id}")
    public Result deleteSelfApplication(@PathVariable Long id) {
        log.info("用户删除自己的申请: {}", id);
        Result result = applicationsService.deleteSelfApplication(id);
        return result;
    }


}

